package scuts.core;

import scuts.core.types.LazyIterator;

class TakeIterable {
  public static function take<T> (it:Iterable<T>, numElements:Int):Iterable<T> 
	{
		var res = [];
		var i = 0;
		for (e in it) {
			res.push(e);
			
			if (++i == numElements) break;
		}
		return res;
	}
}

class TakeIterator {
  public static function take<T>(iter:Iterator<T>, numElements:Int):Iterator<T>
  {
    var i = 0;
    return 
    {
      hasNext : function () 
        {
          return i < numElements && iter.hasNext();
        },
      next : function () 
        {
          i++;
          return iter.next();
        }
    }
  }
}

class TakeLazyIterator {
  public static function take<T>(iter:LazyIterator<T>, numElements:Int):LazyIterator<T>
  {
    return function () return TakeIterator.take(iter(), numElements);
  }
}